﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace 定时执行上传存储过程1._0
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Form1 s=new Form1();
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtboxIP.Text != "")
            {
                string sql = @"exec sp_addlinkedserver'fsletsserver','','SQLOLEDB','"+txtboxIP.Text.Trim()+"'\n exec sp_addlinkedsrvlogin 'fsletsserver', 'false',null,'"+txtBoxUserid.Text.Trim()+"','"+txtBoxPSW.Text.Trim()+"'";
               // MessageBox.Show(sql);
                exec_sql(sql);
            }
        }


        #region //根据传入的sql语句返回受影响的行数
        
        public int exec_sql(string sql)
        {
            int result = 0;
            s.getsqlconn();
            SqlConnection conn = new SqlConnection(s.Sqlconn);
            try
            {


                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandTimeout = 50000;
                conn.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                conn.Close();
            }
            if (result > 0)
            {
                MessageBox.Show("操作成功！影响到" + result.ToString() + "行！");
            }
            return result;
        } 
        #endregion

        //删除所有链接服务器
        private void button2_Click(object sender, EventArgs e)
        {
            string sql = @"exec sp_dropserver 'fsletsserver','droplogins'";
            exec_sql(sql);
        }

        #region 测试链接是否生效
        private void button8_Click(object sender, EventArgs e)
        {
            s.getsqlconn();
            SqlConnection conn = new SqlConnection(s.Sqlconn);
            conn.Open();
            try
            {
                SqlDataAdapter huoche = new SqlDataAdapter("SELECT database_id as 序号,name as 数据库名 FROM [fsletsserver].[Master].sys.databases ", conn);
                DataSet ds = new DataSet();
                huoche.Fill(ds, "123");
                dataGridView1.DataSource = ds.Tables["123"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        } 
        #endregion

        #region "创建table_all表sql语句"
        public string table_all =
 @"CREATE TABLE [dbo].[Table_all](
	[id] [int] NOT NULL,
	[sqlstr] [varchar](4000) NULL,
	[name] [varchar](50) NULL,
	[sqlwhere] [varchar](100) NULL,
	[columnkey] [varchar](20) NULL,
	[timestampflag] [binary](8) NULL,
	[flag] [int] NULL,
	[sqlstr1] [varchar](4000) NULL
) ON [PRIMARY]
INSERT [dbo].[Table_all] ([id], [sqlstr], [name], [sqlwhere], [columnkey], [timestampflag], [flag], [sqlstr1]) VALUES (1, N'code,name,syscode,otherkm1,otherkmname1,otherkm2,otherkmname2,paynamev,fifsysset,parentcode,sale_kinds1,fEntityNo,FClub,Fpcno,timestampflag', N'syskindacc', N'code+FClub', N'code', NULL, 1, N'code+FClub,name,syscode+FClub,otherkm1,otherkmname1,otherkm2,otherkmname2,paynamev,fifsysset,parentcode,sale_kinds1,fEntityNo,FClub,Fpcno,timestampflag')
INSERT [dbo].[Table_all] ([id], [sqlstr], [name], [sqlwhere], [columnkey], [timestampflag], [flag], [sqlstr1]) VALUES (2, N'code,name_c,if_efficieny,type,parent_code,if_sat,name_e,if_account,account_code,memo,money_type,payment_type,bank_service,parent_type,detail_type,type_workshop,type_code,touch_disp,fifrepacc,Fclub,timestampflag', N'sys_payment', N'code+Fclub', N'code', NULL, 1, N'code+Fclub,name_c,if_efficieny,type,parent_code+Fclub,if_sat,name_e,if_account,account_code,memo,money_type,payment_type,bank_service,parent_type,detail_type,type_workshop,type_code,touch_disp,fifrepacc,Fclub,timestampflag')
INSERT [dbo].[Table_all] ([id], [sqlstr], [name], [sqlwhere], [columnkey], [timestampflag], [flag], [sqlstr1]) VALUES (3, N'billcode,billtype,rec_status,bill_status,pay_status,billshop,saleshop,payshop,billdate,opentime,closedate,closetime,openuserid,closeuserid,dailysno,tableno,tableno_ext,noofguest,serverno,periodno,shiftno,closeshift,parentshop,grand_billcode,checkin_id,cardno,memno,memid,guest_identity,name,sex,billtotal,discountpercent,discounttotal,gratuitypercent,gratuitytotal,taxpercent,taxtotal,tipstotal,rebatetotal,totalpayable,paymethod,paydesp,printno,invoiceno,invoiceflag,generateflag,payflag,reception,saletype,lockstatus,sttime,endtime,fcount_hh,fcount_mm,fcditemno,otherstatus,club_id,memsid,sequence,keyno,modify,modifytime,clubcode,Fpcno,FClub,fEntityNo,ch_payno,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,is_check,bm_ck,dzxh,checker,rq_check,cardbalance,zb_bm_ck,depositall,fcusttdno,syscode,f_paykind,fRemark,saleid,paycode,accuserid,fbillnohbaccno,fhbbillflash,fRemarkdoc,quitydje,ydallje,fydbillno,finmpyj,timestampflag', N'pos_bills', N'billcode+''LX''', N'billcode', NULL, 1, N'billcode+''LX'' as billcode,billtype,rec_status,bill_status,pay_status,billshop,saleshop,payshop,billdate,opentime,closedate,closetime,openuserid,closeuserid,dailysno,tableno,tableno_ext,noofguest,serverno,periodno,shiftno,closeshift,parentshop,grand_billcode+''LX'' as grand_billcode,checkin_id,cardno,memno,memid,guest_identity,name,sex,billtotal,discountpercent,discounttotal,gratuitypercent,gratuitytotal,taxpercent,taxtotal,tipstotal,rebatetotal,totalpayable,paymethod+Fclub,paydesp,printno,invoiceno,invoiceflag,generateflag,payflag,reception,saletype,lockstatus,sttime,endtime,fcount_hh,fcount_mm,fcditemno,otherstatus,club_id,memsid,sequence,keyno,modify,modifytime,clubcode,Fpcno,FClub,fEntityNo,ch_payno,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,is_check,bm_ck,dzxh,checker,rq_check,cardbalance,zb_bm_ck,depositall,fcusttdno,syscode,f_paykind,fRemark,saleid,paycode+Fclub,accuserid,fbillnohbaccno,fhbbillflash,fRemarkdoc,quitydje,ydallje,fydbillno,finmpyj,timestampflag')
INSERT [dbo].[Table_all] ([id], [sqlstr], [name], [sqlwhere], [columnkey], [timestampflag], [flag], [sqlstr1]) VALUES (4, N'saleid,billcode,grand_billcode,rec_status,sale_status,pay_status,sale_type,billshop,saleshop,payshop,produceshop,saledate,saletime,closedate,paydate,userid,itemcode,itemname1,itemname2,itemspec,saleprice,quantity,saletotal,discountpercent,discounttotal,gratuitypercent,gratuitytotal,rebatetotal,taxpercent,taxtotal,totalpayable,saleweight,stockprice,stockqty,modifier_memo,serverno,if_discount,if_gratuity,sale_prop,if_revenue,if_generate,if_other,if_invoice,closemethod,paymethod,checkin_id,orderno,seatno,printno,otherid,if_import,if_tax,pricemethod,sale_kinds,parent_kinds,sale_category,parent_category,sale_special,parent_special,stat_code,paymentid,fit_paymentid,payflag,itemid,autocdflag,otherflag,complimentary_flag,modifier,reason,kitchen_type,seat_no,is_meal,is_backup,meal_id,meal_type,is_inv,is_addtion,club_id,holidaymethod,clubcode,sttime,endtime,fcount_hh,fcount_mm,fcditemno,Fpcno,FClub,fEntityNo,fit_saleid,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,xh,dzxh,dj_cb,je_cb,bm_ck,is_check,walkcust_ftype,keyno,memno,Fremark,deposit,fcusttdno,fbillNosub,disccode,spandhyacc,ch_printflag,accuserid,fdayandhousr,fmpdaycount,zbcardno,v_artno,Fmp_m_id,acc_clubkinds1,acc_clubkinds2,acc_clubkinds3,timestampflag', N'pos_sales', N'billcode+''LX''', N'billcode', NULL, 1, N'cast(convert(varchar(24),saleid)+''88'' as numeric(24,0)) as saleid,billcode+''LX'' as billcode,grand_billcode+''LX'' as grand_billcode,rec_status,sale_status,pay_status,sale_type,billshop,saleshop,payshop,produceshop,saledate,saletime,closedate,paydate,userid,itemcode,itemname1,itemname2,itemspec,saleprice,quantity,saletotal,discountpercent,discounttotal,gratuitypercent,gratuitytotal,rebatetotal,taxpercent,taxtotal,totalpayable,saleweight,stockprice,stockqty,modifier_memo,serverno,if_discount,if_gratuity,sale_prop,if_revenue,if_generate,if_other,if_invoice,closemethod+Fclub,paymethod+Fclub,checkin_id,orderno,seatno,printno,otherid,if_import,if_tax,pricemethod,sale_kinds+Fclub,parent_kinds+Fclub,sale_category+Fclub,parent_category+Fclub,sale_special,parent_special,stat_code,paymentid,fit_paymentid+''LX'' as fit_paymentid,payflag,itemid,autocdflag,otherflag,complimentary_flag,modifier,reason,kitchen_type,seat_no,is_meal,is_backup,meal_id,meal_type,is_inv,is_addtion,club_id,holidaymethod,clubcode,sttime,endtime,fcount_hh,fcount_mm,fcditemno,Fpcno,fclub,fEntityNo,fit_saleid+''LX'' as fit_saleid,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,xh,dzxh,dj_cb,je_cb,bm_ck,is_check,walkcust_ftype,keyno,memno,Fremark,deposit,fcusttdno,fbillNosub,disccode,spandhyacc,ch_printflag,accuserid,fdayandhousr,fmpdaycount,zbcardno,v_artno,Fmp_m_id,acc_clubkinds1,acc_clubkinds2,acc_clubkinds3,timestampflag')
INSERT [dbo].[Table_all] ([id], [sqlstr], [name], [sqlwhere], [columnkey], [timestampflag], [flag], [sqlstr1]) VALUES (5, N'paymentid,billcode,rec_status,pay_status,paymethod,payparent,payamount,payforeign,payshop,paydate,payflag,payrate,payfamt,paychanges,paycardno,ExpiryDate,paysysid,payname,userid,shiftno,if_invoice,if_revenue,payno,paymemo,paytype,memid,clubcode,fit_paymentid,fEntityNo,FClub,Fpcno,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,syscode,paycode,sale_kinds,sale_category,sale_special,fifrepacc,fifdhyhpay,timestampflag', N'pos_bill_payment', N'billcode+''LX''', N'billcode', NULL, 1, N'cast(convert(varchar(24),paymentid)+''88'' as numeric(24,0)) as paymentid,billcode+''LX'' as billcode,rec_status,pay_status,paymethod+Fclub,payparent+Fclub,payamount,payforeign,payshop,paydate,payflag,payrate,payfamt,paychanges,paycardno,ExpiryDate,paysysid,payname,userid,shiftno,if_invoice,if_revenue,payno,paymemo,paytype,memid,clubcode,fit_paymentid+''LX'' as fit_paymentid,fEntityNo,FClub,Fpcno,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,syscode,paycode+Fclub,sale_kinds,sale_category,sale_special,fifrepacc,fifdhyhpay,timestampflag')
INSERT [dbo].[Table_all] ([id], [sqlstr], [name], [sqlwhere], [columnkey], [timestampflag], [flag], [sqlstr1]) VALUES (6, N'sid,cardno,cname,sex,mem_status,mem_type,checkin_date,checkin_time,operator,checkin_status,if_towel,if_keyring,keyno,if_returnkey,checkouttime,memo,color,memiden,memid,workshop,expendcard,deposit,guesttype,issuecard,lockstatus,clubcode,posflag,fEntityNo,FClub,Fpcno,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,fmp,ifinshoes,ifshoes,shoesdate1,fbillNo,fmpdate,shoesdate2,ifoutshoes,fgphzs,fbphzs,fsalempdttime,ifcusttype,fje,fqry,fcusttdno,fbillNosub,city_id,fmpsttdate,fmpenddate,fmpusermin,fmpdaycount,shoesdate4,fmpjeall,fyjjeall,finje,ftime_mode,timestampflag', N'fitness_checkin', N'sid+''88''', N'sid', NULL, 1, N'sid+''88'' as sid,cardno,cname,sex,mem_status,mem_type,checkin_date,checkin_time,operator,checkin_status,if_towel,if_keyring,keyno,if_returnkey,checkouttime,memo,color,memiden,memid,workshop,expendcard,deposit,guesttype,issuecard,lockstatus,clubcode,posflag,fEntityNo,FClub,Fpcno,fcreatedate,fmodifydate,fUserNo,n_last,ifdown,fmp,ifinshoes,ifshoes,shoesdate1,isnull(fbillno+''LX'','''') as fbillno,fmpdate,shoesdate2,ifoutshoes,fgphzs,fbphzs,fsalempdttime,ifcusttype,fje,fqry,fcusttdno,isnull(fbillnosub+''LX'','''') as fbillnosub,city_id,fmpsttdate,fmpenddate,fmpusermin,fmpdaycount,shoesdate4,fmpjeall,fyjjeall,finje,ftime_mode,timestampflag')

";
        #endregion
        private void button3_Click(object sender, EventArgs e)
        {
            exec_sql(table_all);
        }
        //替换table_all表的相关需要唯一的字段
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox3.TextLength == 2 && textBox4.TextLength >= 2&&textBoxFfclub.TextLength>=2)
            {
                string sql =
                       @"update Table_all set sqlwhere=REPLACE(sqlwhere,'88','" + textBox3.Text.Trim() + "'),sqlstr1=REPLACE(sqlstr1,'88','" + textBox3.Text.Trim() + "') ";
                sql += @"update Table_all set sqlwhere=REPLACE(sqlwhere,'LX','" + textBox4.Text.Trim() + "'),sqlstr1=REPLACE(sqlstr1,'LX','" + textBox4.Text.Trim() + "') ";
                sql += @"update Table_all set sqlstr1=REPLACE(sqlstr1,'FClub','''" + textBoxFfclub.Text.Trim() + "''') ";
                sql += @"update Table_all set sqlwhere=REPLACE(sqlwhere,'FClub','''" + textBoxFfclub.Text.Trim() + "''') ";
                sql += @"update syskindacc set Fclub='"+Form1.Bendifclub+"' where Fclub is null ";
                sql += @"update sys_payment set Fclub='" + Form1.Bendifclub + "' where Fclub is null ";
                exec_sql(sql);
            }
            else
            {
                MessageBox.Show("请检查你输入的格式");
            }
        }
        //给工作站相关账务表添加时间戳
        private void button5_Click(object sender, EventArgs e)
        {
            string sql =
                    @"alter table pos_bills add timestampflag timestamp
                    alter table pos_sales add timestampflag timestamp
                    alter table pos_bill_payment add timestampflag timestamp
                    alter table fitness_checkin add timestampflag timestamp
                    alter table syskindacc add timestampflag timestamp
                    alter table sys_payment add timestampflag timestamp";
            exec_sql(sql);
        }

        private void button6_Click(object sender, EventArgs e)
        {
           string  sqlconn = @"server=" + txtboxIP.Text.Trim() + ";database = " + txtBoxDBname.Text.Trim() + "; uid =" + txtBoxUserid.Text.Trim() + "; pwd = " + txtBoxPSW.Text.Trim() + ";";
           string sql =
            @"alter table pos_bills add timestampflag binary(8) 
            alter table pos_sales add timestampflag binary(8)  
            alter table pos_bill_payment add timestampflag binary(8) 
            alter table fitness_checkin add timestampflag binary(8) 
            alter table syskindacc add timestampflag binary(8) 
            alter table sys_payment add timestampflag binary(8) 
            alter table pos_bills alter column paymethod varchar(20)  
            alter table pos_sales alter column closemethod varchar(20)  
            alter table pos_sales alter column paymethod varchar(20)  
            alter table pos_bill_payment alter column paymethod varchar(20)  
            alter table pos_bill_payment alter column payparent varchar(20) 
            alter table pos_sales alter column sale_kinds varchar(20)  
            alter table pos_sales alter column parent_kinds varchar(20)  
            alter table pos_sales alter column sale_category varchar(20)  
            alter table pos_sales alter column parent_category varchar(20)  ";

           SqlConnection conn = new SqlConnection(sqlconn);

           int result = 0;

           try
           {


               SqlCommand cmd = new SqlCommand(sql, conn);
               conn.Open();
               result = cmd.ExecuteNonQuery();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

           finally
           {
               conn.Close();
           }
           if (result > 0)
           {
               MessageBox.Show("添加时间戳成功！影响到" + result.ToString() + "行！");
           }


        }

        //private static string p_up_table_all=
        //    @"--创建上传储存过程p_up_table_all/nif exists (select * from sysobjects where id = object_id(N'[p_up_table_all]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)/n drop PROCEDURE [p_up_table_all]/n GO/n CREATE proc [dbo].[p_up_table_all]   /n@fclub varchar(10)--分公司编号/nas /nDECLARE @SQL NVarchar(4000)/nDECLARE tables_cursor CURSOR STATIC /n	FOR/n    select [name],[sqlstr],[sqlstr1],[sqlwhere],[columnkey],[timestampflag] from table_all where flag=1 --and [name]='fitness_checkin'  --flag=0为需下载的表,1为需上传的表/n	OPEN tables_cursor/n /n DECLARE @Name varchar(50),@sqlstr varchar(4000),@sqlstr1 varchar(4000),@sqlwhere varchar(100),@columnkey varchar(20),@timestampflag binary(8) --声明局部变量/n/nfetch next from tables_cursor into @Name,@sqlstr,@sqlstr1,@sqlwhere,@columnkey,@timestampflag/nwhile(@@fetch_status=0)/nbegin/n Declare @ts binary(8)/n/n----判断表记录是否为空/nif @timestampflag is null/n begin/n  SET @SQL = 'insert into fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.['+@Name+']('+@sqlstr+') select '+@sqlstr1+' from ['+@Name+'] where fclub='''+@fclub+''''/n PRINT '正在更新表' + @name + '中的记录...'/n exec sp_executesql @SQL/n print @Name + '表记录更新完成。'/n set @SQL = 'set @ts=(select max(timestampflag) from '+ @name +')'/n exec sp_executesql @SQL,N'@ts binary(8) out',@ts out/n --更新set_serialno表no字段/n --update fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.set_serialno set no = b.no from set_serialno b,fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.set_serialno a  where a.code = b.code and a.fclub=b.fclub and b.code=@sqlwhere and b.fclub=@fclub/n --print @ts 添加时间戳到table_all表/n update table_all set timestampflag=@ts where [name] = @name/n/n --print @sql/n set @SQL = ''/n /nend/n/nelse/n begin/n  DECLARE @re bit/n  set @SQL = 'set @re=case when exists(select 1 from '+@Name+' where fclub='''+@fclub+''' and timestampflag > (select timestampflag from table_all where [name]='''+@Name+''')) then 1 else 0 end'  /n  --print @Sql/n  exec sp_executesql @SQL,N'@re bit out',@re out/n  set @SQL = ''/n/n if @re=1/n    /n  begin/n    SET @SQL = 'DELETE fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.['+@Name+'] where '+@columnkey+' in (select '+@sqlwhere+' from '+@Name+' where  timestampflag > (select timestampflag from table_all where [name]='''+@Name+''') and fclub='''+@fclub+''');'/n    SET @SQL = @SQL + 'insert into fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.['+@Name+']('+@sqlstr+') select '+@sqlstr1+' from ['+@Name+'] where '+@sqlwhere+' not in(select '+@columnkey+' from fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.['+@Name+'] where fclub='''+@fclub+''') and timestampflag > (select timestampflag from table_all where [name]='''+@Name+''') and fclub='''+@fclub+''';'/n  end/nend/n IF @SQL <> ''/n begin/n  PRINT '正在更新表' + @name + '中的记录...'/n --print @sql/n exec sp_executesql @SQL/n print @Name + '表记录更新完成。'/n set @SQL = 'set @ts=(select max(timestampflag) from '+ @name +')'/n exec sp_executesql @SQL,N'@ts binary(8) out',@ts out/n  --更新set_serialno表no字段/n --update fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.set_serialno set no = b.no from set_serialno b,fsletsserver."+txtBoxDBname.Text.Trim()+".dbo.set_serialno a  where a.code = b.code and a.fclub=b.fclub and b.code=@sqlwhere and b.fclub=@fclub/n--print @ts 添加时间戳到table_all表 /nupdate table_all set timestampflag=@ts where [name] = @name/n --print @ts/nend/n fetch next from tables_cursor into @Name,@sqlstr,@sqlstr1,@sqlwhere,@columnkey,@timestampflag/nend/nclose tables_cursor /ndeallocate tables_cursor/n/n/n/n";

        private void button7_Click(object sender, EventArgs e)
        {
            
           exec_sql(richTextBox1.Text);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (txtBoxDBname.Text == "" || txtBoxDBname.Text == "必填")
            {
                MessageBox.Show("未填写数据库名字！");
                return;
            }
                else
                {
           richTextBox1.Text= richTextBox1.Text.Replace("fsletsFWQ", txtBoxDBname.Text);
                }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //删除table_all表
            exec_sql("DROP TABLE Table_all");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //删除工作站相关账务表的时间戳
            string sql_del_timestamp = @"
            alter table pos_bills DROP COLUMN timestampflag 
            alter table pos_sales DROP COLUMN timestampflag 
            alter table pos_bill_payment DROP COLUMN timestampflag 
            alter table fitness_checkin DROP COLUMN timestampflag
            alter table syskindacc DROP COLUMN timestampflag
            alter table sys_payment DROP COLUMN timestampflag";

            exec_sql(sql_del_timestamp);

        }

        private void button12_Click(object sender, EventArgs e)
        {
            //删除上传存储过程
            string sql_del_proc = @"drop proc p_up_table_all";
            exec_sql(sql_del_proc);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            //删除服务器相关账务表的时间戳
            string sql_del_timestamp = @"
            alter table pos_bills DROP COLUMN timestampflag 
            alter table pos_sales DROP COLUMN timestampflag 
            alter table pos_bill_payment DROP COLUMN timestampflag 
            alter table fitness_checkin DROP COLUMN timestampflag
            alter table syskindacc DROP COLUMN timestampflag
            alter table sys_payment DROP COLUMN timestampflag";

            exec_sql_del_fuwuqi_timestamp(sql_del_timestamp);
            
        }

        #region //根据传入的sql语句返回受影响的行数

        public int exec_sql_del_fuwuqi_timestamp(string sql)
        {
            int result = 0;
            string sql_conn = @"server=" + txtboxIP.Text + ";database = " + txtBoxDBname.Text + "; uid =" + txtBoxUserid.Text + "; pwd = " + txtBoxPSW.Text + ";";
            SqlConnection conn = new SqlConnection(sql_conn);
            try
            {


                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandTimeout = 50000;
                conn.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                conn.Close();
            }
            if (result > 0)
            {
                MessageBox.Show("操作成功！影响到" + result.ToString() + "行！");
            }
            return result;
        }
        #endregion

        private void button14_Click(object sender, EventArgs e)
        {
            //初始化服务器账务
            string Init_server_sales_data =
            @"truncate table pos_sales
            truncate table pos_bills
            truncate table pos_bill_payment
            truncate table fitness_checkin
            truncate table syskindacc
            drop table sys_payment ";
            Init_server_sales_data += 
            @"CREATE TABLE [dbo].[sys_payment](
	        [code] [nvarchar](20) NOT NULL,
	        [name_e] [nvarchar](50) NULL,
	        [name_c] [nvarchar](50) NULL,
	        [if_account] [nchar](1) NULL,
	        [if_efficieny] [nchar](1) NULL,
	        [account_code] [nvarchar](50) NULL,
	        [type] [int] NULL,
	        [memo] [nvarchar](200) NULL,
	        [parent_code] [nvarchar](20) NULL,
	        [money_type] [nvarchar](50) NULL,
	        [payment_type] [nchar](1) NULL,
	        [bank_service] [decimal](18, 4) NULL,
	        [parent_type] [nvarchar](10) NULL,
	        [detail_type] [nvarchar](10) NULL,
	        [if_sat] [nchar](1) NULL,
	        [type_workshop] [nvarchar](200) NULL,
	        [type_code] [nvarchar](200) NULL,
	        [touch_disp] [nvarchar](30) NULL,
	        [fEntityNo] [varchar](10) NULL,
	        [FClub] [varchar](12) NULL,
	        [Fpcno] [varchar](10) NULL,
	        [fcreatedate] [datetime] NULL,
	        [fmodifydate] [datetime] NULL,
	        [fUserNo] [varchar](8) NULL,
	        [n_last] [numeric](18, 0) NULL,
	        [ifdown] [char](1) NULL,
	        [fcoupinput] [int] NULL,
	        [fifrepacc] [int] NULL,
         CONSTRAINT [PK_sys_payment] PRIMARY KEY CLUSTERED 
        (
	        [code] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]";

            if (MessageBox.Show("将会初始化所有账务记录?是否继续？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                exec_sql_del_fuwuqi_timestamp(Init_server_sales_data);

            }
           
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (button14.Enabled == false)
            {
                button14.Enabled = true;
            }
            else
            {
                button14.Enabled = false;
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //MessageBox.Show(Form1.Bendifclub);
            txtboxIP.Text = Properties.Settings.Default.server_ip;
            txtBoxPSW.Text = Properties.Settings.Default.pwd;
            //txtBoxDBname.Text = Properties.Settings.Default.db_name;
            //textBoxFfclub.Text = Properties.Settings.Default.Ffclub;
            txtBoxDBname.Text = Form1.ZongbuDB;
            textBoxFfclub.Text = Form1.Zongbufclub;
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.server_ip = txtboxIP.Text;
            Properties.Settings.Default.pwd = txtBoxPSW.Text;
            Properties.Settings.Default.db_name = txtBoxDBname.Text;
            Properties.Settings.Default.Ffclub = textBoxFfclub.Text;
            Properties.Settings.Default.Save();
        }

       
        
    }
}
